const fs = require('fs')
const querystring = require('querystring');
let value = fs.readFileSync('persons.json').toString()
value = JSON.parse(value)


function login(user1) {

    let user = user1
    // 登录
    let admin = value.find(item => user.username === item.username && user.password === item.password)
    if (admin) {
        return '登录成功'

    } else {
        return '用户名或密码错误'

    }
}
function register(user1) {
    // let user = { username: "admin1", password: "12345" }
    // let user = {"username":"xm3","password":"333"}
    let user = user1
    // 注册
    let tag = value.find(item => item.username === user.username)
    if (tag) {
        // console.log('该用户已注册');
        return '该用户已注册'
        //  document.write('该用户已注册')
    } else {

        value.push(user)
        fs.writeFileSync('persons.json', JSON.stringify(value), { flag: 'w' })
        // console.log(value);
        return '注册成功'
    }
}
module.exports = {
    login,
    register
}